package velha.connection.bt;

import javax.microedition.lcdui.*;
import velha.VelhaMIDlet;

public class GUIServidor implements CommandListener, Runnable {

    private final Command COMANDO_CANCELAR = new Command("Cancelar", Command.CANCEL, 0);
    private final Form telaEspera = new Form("Criar jogo");
    private BTServidor bt_servidor;
    //
    public final VelhaMIDlet midlet;

    public GUIServidor(VelhaMIDlet midlet) {
        this.midlet = midlet;

        telaEspera.addCommand(COMANDO_CANCELAR);
        telaEspera.setCommandListener(this);

        new Thread(this).start();
    }

    public void commandAction(Command c, Displayable d) {
        if (c == COMANDO_CANCELAR) {
            bt_servidor.destroy();
            midlet.getDisplay().setCurrent(new BTMenu(midlet));
        }
    }

    public void completeInitialization(boolean btReady) {
        // bluetooth iniciado
        if (btReady) {
            telaEspera.append("Aguardando jogador...");
        } else {
            telaEspera.append("Falha ao iniciar bluetooth");
        }
        midlet.getDisplay().setCurrent(telaEspera);

        // something wrong
        //  Alert al = new Alert("Erro", "Falha ao iniciar bluetooth", null, AlertType.INFO);
        //al.setTimeout(Alert);
        // midlet.getDisplay().setCurrent(al);
    }

    public void run() {
        bt_servidor = new BTServidor(this);
    }
}
